/*
 * CS_WORLD_BOSS_PARTY_READY.cpp
 *
 *  Created on: 2014. 1. 13.
 *      Author: jjaehuny
 */

#include "../../Command.h"

#include "../../User.h"
#include "../../NLogic/Space.h"
#include "../../NLogic/Party.h"
#include "../../NLogic/Stamina.h"


namespace ProjectZ { namespace NCommand {

//static void SendAck_CS_WORLD_BOSS_PARTY_READY(User * pUser, int16_t ack)
//{
//	::Network::Packet * pAck = ::Network::GETPACKETPOOL()->SEND_NEW();
//	* pAck << static_cast<int16_t>(ack);
//	pAck->MakePacket(SC_WORLD_BOSS_PARTY_INVITE_RES);
//	pUser->Send(pAck);
//}

void Command::CS_WORLD_BOSS_PARTY_READY(User * pUser, ::Network::Packet * pPacket)
{
	/*
	/// 방장을 포함한 모든 유저가 준비 되면 ready를 올림
	CS_WORLD_BOSS_PARTY_READY				0x400B
	{

	}

	//수락 성공 여부, 유저 목록 전송
	SN_PARTY_CREATE_DONE				0x111f
	{
		s16	ack;				/// 1:완료 ,  -1 실패
		u8	count				/// 수락한 유저 수
		{
			u32	u32gid;			/// 수락한 유저의 gid
		}
	}
	 *
	 */

	NLogic::Space * pSpace = pUser->GetSpace();
	if (NULL == pSpace)
	{
		DEBUGLOG("%s NULL == pSpace", __FUNCTION__);
		//SendAckFail(pUser, ACK_NO_SPACE);
		return;
	}

	if (false == pSpace->IsWorldBoss())
	{
		DEBUGLOG("%s pSpace->GetData()._spaceType != NLogic::Space::SpaceType::WORLDBOSS_PARTY", __FUNCTION__);
		return;
	}

	if (pUser->GetEnumState() != User::EnumState::ACCEPT_WAIT)
	{
		DEBUGLOG("%s pUser->GetEnumState() != User::EnumState::READY_WAIT", __FUNCTION__);
		return;
	}

	NLogic::Party * pParty = pSpace->GetParty();
	if (NULL == pParty)
	{
		DEBUGLOG("%s NULL == pParty", __FUNCTION__);
		return;
	}

	if (NLogic::Party::State::WORLD_BOSS_WAIT != pParty->GetState() && NLogic::Party::State::WORLD_BOSS_WAIT_FRIEND != pParty->GetState())
	{
		DEBUGLOG("%s NLogic::Party::State::WORLD_BOSS_WAIT != pParty->GetState()", __FUNCTION__);
		return;
	}

	auto inviteItr = pParty->GetInviteUserTree().find(pUser->GetUserSeq());
	if (inviteItr == pParty->GetInviteUserTree().end())
	{
		DEBUGLOG("%s inviteItr == pParty->GetInviteUserTree().end()", __FUNCTION__);
		return;
	}

	auto acceptItr = pParty->GetAcceptUserTree().find(pUser->GetUserSeq());
	if (acceptItr != pParty->GetAcceptUserTree().end())
	{
		DEBUGLOG("%s acceptItr != pParty->GetAcceptUserTree().end()", __FUNCTION__);
		return;
	}


	pParty->AddAcceptUserTree(pUser);
	if (static_cast<int>(pParty->GetAcceptUserTree().size()) < pSpace->GetData()._maxUser + 1)
	{
		DEBUGLOG("%s acceptItr != pParty->GetAcceptUserTree().end()", __FUNCTION__);
		return;
	}

	auto partyAllUserTree = pParty->GetAllUserTree();

	::Network::Packet * pNoti = ::Network::GETPACKETPOOL()->SEND_NEW();
	* pNoti << static_cast<int16_t>(ACK_OK);
	* pNoti << static_cast<uint8_t>(partyAllUserTree.size());

	std::for_each(partyAllUserTree.begin(), partyAllUserTree.end(), [&](NLogic::Party::USERTREE::value_type & pair) {
		User * pUser = pair.second;

		uint32_t u32gid = static_cast<uint32_t>(pUser->GetUserSeq());

		* pNoti << u32gid;

		pUser->SetEnumState(User::EnumState::READY_WAIT);

		if (NULL != pUser->GetStamina())
			pUser->GetStamina()->DecreaseKey(1);
//			pUser->GetCharacterInfo().event_stamina -= 1;
	} );

	pNoti->MakePacket(CMD_SN_PARTY_CREATE_DONE);

	pSpace->SendAll(pNoti);

	pParty->SetState(NLogic::Party::State::READY_WAIT);
}

} /*NCommand*/ } /*ProjectZ*/

